//Functions

//Canvas Functions ================================================================================
function clear(theCanvas) {
  theCanvas.clearRect(0, 0, WIDTH, HEIGHT);
}
//Preloading Images Functiona
function imageLoaded(){
	imagesLoaded++;
}
function checkLoaded(){
	if(imagesLoaded==imagesToLoad){//TOTAL ASSETS TO LOAD HERE
		clearInterval(imageLoadInterval);
		bc.drawImage(scr_game,0,0);//draws game background to background canvas
		titleScroll();
	}
}
//Title Screen Functions ==========================================================================
function titleScroll(){
	gameState = 0;
	clear(fc);
	if(titleScrollInt>2) titleScrollInt = 0;
	fc.drawImage(scr_title[titleScrollInt],0,0);
	if(titleScrollInt==0) titleTimer = setTimeout(titleScroll, 4000);
	if(titleScrollInt==1) titleTimer = setTimeout(titleScroll, 4000);
	if(titleScrollInt==2) titleTimer = setTimeout(titleScroll, 10000);
	titleScrollInt++;
}
function gameTransition(){
	t = t + 10;
	fc.clearRect(
		WIDTH/2-(t*1.17176)/2
		,HEIGHT/2-t/2
		, t*1.17176, t);
	transitionTimer = setTimeout(gameTransition, 20);
	if(t>450){
		clearTimeout(transitionTimer);
		clear(fc);
		fc.drawImage(scr_frame, 0, 0);
		gameState = 1;
		clear(gc);
		gameEngine();
	}
	
}
//Chicken Movement Functions ======================================================================
function moveChicken(){
	if(cS==0){//set chicken state
		cX = Math.floor(Math.random()*(WIDTH-90))+20;
		dX = 0;
		cY = -75;
		cS = 1;
		feed = 100;
		forceFly = 0;
		upPressed = false;
	}
	clear(gc);
	//Check for a Collision
	isCollided = checkCollision();
	if(isCollided && cS!= 3){		
		if(dY>LANDINGTHRESHOLD){//This is where BOUNCING is initiated
			dY = -(Math.floor(dY*1.5))	//set Y for bouncing arc
			bounceLock = Math.floor(Math.random()*6)+17;//set the amount of time for paralyzation due to a bounce
			if(dX==0) {	//set x direction if chicken isnt already moving
				if(Math.floor(Math.random()*2)==1){dX = 5;}else{dX = -5;}
			}
			cS = 2;	//bouncing state			
		}else{
			cS = 3;//set chicken STATE to 3 = Landed
			score = score + (10*DIFFICULTY) + (feed*DIFFICULTY);
			landed++;
			if(landed==2) feedSpeed = 3;
			if(landed==5) feedSpeed = 2;
			if(landed==7) feedSpeed = 1;
		}
	}
	
	if(cS==1){//state of normal chicken movement
	//x-axis movement
		if(rightPressed && feed > 0){dX+= 1;}
		if(leftPressed && feed > 0){dX-= 1;}
		if(downPressed && feed > 0){dX = 0;}
		if(dX > 6){dX = 7;}
		if(dX <-6){dX =-7;}
		cX += dX;
	//y-axis movement
		if(!upPressed || feed < 1) dY+=1;
		if(upPressed && feed > 0){//upward velocity depending on where you are in the atmosphere
			if(cY > GRAVITYLINE) dY+=-1;
			if(cY <=GRAVITYLINE) dY+=-2;
		}
		if(dY<-16){dY =-16;}
		if(dY> TERMINALVELOCITY){dY = TERMINALVELOCITY;}
		cY += dY;
		
		if(cY < -200){cY = -200;}//upper ceiling for chicken flight
	//x-axis bounds
		if(cX < 1){cX = WIDTH - cW;}
		if(cX+cW > WIDTH){cX = 1;}
	//feed subtraction
		feedCount++;
		if((dY<0 || upPressed) && feed > 0 && feedCount==feedSpeed) feed = feed - (1+Math.floor(landed/5));
		if(feedCount>=feedSpeed) feedCount = 0;
		if(feed<0) feed = 0;
	//forceFly
		if(forceFly>0){
			forceFly--;
			upPressed = true;
			if(forceFly==0) upPressed = false;
		}	
	}//close of normal chicken movement
	if(cS==2){//chicken BOUNCING state
		dY++;
		cY = cY + dY;
		cX = cX + dX;
		bounceLock--;
		if(bounceLock<1){
			if(upPressed || downPressed || leftPressed || rightPressed){
				cS = 1;
				cAnimateFrame = 0;//reset the animation frame
			}
		}
	}//close bouncing state
	if(cS==3){/*chicken LANDED state*/}
	//Check for ground
	if(cY+75 > 400){
		setExposion();
		gameState = 3;
	}
	//Draw AstroChicken
	drawChicken();
}
function checkCollision(){//CHECK COLLISION
	if(cX < PAD_X+PAD_WIDTH && cX+cW > PAD_X && cY < PAD_Y+PAD_HEIGHT && cY+cH > PAD_Y){
		return true;
	}
	return false;
}
function drawChicken(){//DRAWS & ANIMATES THE VARIOUS VISUALIZATIONs OF THE CHICKEN
	cAnimateTimer++;
	if(cAnimateTimer>6){
		cAnimateTimer = 0;
		cAnimateFrame++;
		if(cS==1 && cAnimateFrame>1) cAnimateFrame = 0;
		if(cS==2 && cAnimateFrame>3) cAnimateFrame = 0;
		if(cS==3 && cAnimateFrame>30) cAnimateFrame = 0;
	}
	if(cS==1){//State = 1. MOVING
		if(dX > 0 && dY < 0){//flying up & right
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 0, 75, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 63, 75, 63, 75, cX, cY, 63, 75);
		}
		if(dX > 0 && dY >=0){//flying down & right
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 0, 75, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 63, 75, 63, 75, cX, cY, 63, 75);
		}
		if(dX < 0 && dY < 0){//flying up & left
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 0, 150, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 63, 150, 63, 75, cX, cY, 63, 75);
		}
		if(dX < 0 && dY>= 0){//flying down & left
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 0, 150, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 63, 150, 63, 75, cX, cY, 63, 75);
		}	
		if(dX==0 && dY< 0){//flying up
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 63, 0, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 126, 0, 63, 75, cX, cY, 63, 75);
		}
		if(dX==0 && dY>=0){
			gc.drawImage(spr_chicken, 0, 0, 63, 75, cX, cY, 63, 75);
		}		
	}
	if(cS==2){//State = 2. BOUNCING
		if(dX>0){
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 63, 0, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 63, 225, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==2) gc.drawImage(spr_chicken, 0, 225, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==3) gc.drawImage(spr_chicken, 126, 225, 63, 75, cX, cY, 63, 75);			
		}
		if(dX<=0){
			if(cAnimateFrame==0) gc.drawImage(spr_chicken, 63, 0, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==1) gc.drawImage(spr_chicken, 126, 225, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==2) gc.drawImage(spr_chicken, 0, 225, 63, 75, cX, cY, 63, 75);
			if(cAnimateFrame==3) gc.drawImage(spr_chicken, 63, 225, 63, 75, cX, cY, 63, 75);			
		}
	}
	if(cS==3){//State = 3. LANDED
		if(cAnimateFrame < 5 || cAnimateFrame > 6) gc.drawImage(spr_chicken, 126, 150, 63, 75, cX, cY, 63, 75);
		if(cAnimateFrame > 4 && cAnimateFrame < 7) gc.drawImage(spr_chicken, 126, 75, 63, 75, cX, cY, 63, 75);
		
		if(cAnimateFrame > 4 && cAnimateFrame <=5) gc.drawImage(spr_bacock, 0, 0, 207, 100, cX+33, cY-100, 207, 100);
		if(cAnimateFrame > 5 && cAnimateFrame <=6) gc.drawImage(spr_bacock, 0, 100, 207, 100, cX+33, cY-100, 207, 100);
		if(cAnimateFrame > 6) gc.drawImage(spr_bacock, 0, 200, 207, 100, cX+33, cY-100, 207, 100);
		
		if(cAnimateFrame > 12) cS = 0;
	}
}
function setExposion(){
	for(var i=0; i<100; i++){
		eX[i] = (cX)+(Math.floor(Math.random() * 63));
		eY[i] = (cY+50)+(Math.floor(Math.random() * 25));
		eW[i] = Math.floor(Math.random() * 5) + 3;
		eH[i] = Math.floor(Math.random() * 4) + 1;
		
		if(eX[i] > cX+31){
			eDX[i] = Math.floor(Math.random() * 12);
		}else{
			eDX[i] = -Math.floor(Math.random() * 12);
		}
		
		eDY[i] = -Math.floor(Math.random()*16);
		
		var r = Math.floor(Math.random()*4);
		if(r==0){eR[i] = 255; eG[i] = 255; eB[i] = 255;}//white		
		if(r==1){eR[i] = 236; eG[i] = 141; eB[i] =  19;}//orange
		if(r==2){eR[i] = 249; eG[i] = 235; eB[i] =   8;}//yellow
		if(r==3){eR[i] = 255; eG[i] =   0; eB[i] =   0;}//red
	}
	explodeTimer = 25;
	clearInterval(gameInterval);
	gameInterval = setInterval(gameEngine, 50);
	if(soundOn) explosionSnd.play();
}
function explodeChicken(){
	clear(gc);
	for(var i=0; i<100; i++){
		
		if(eDX[i] > 0) eDX[i]--;
		if(eDX[i] < 0) eDX[i]++;
		
		if(eDY[i] < 2) eDY[i]+=2;
		
		eX[i] = eX[i] + eDX[i];
		eY[i] = eY[i] + eDY[i];
		
		if(eY[i] > cY + 75) eY[i] = cY +75
		if(eDY[i]==0){
			eR[i] = 255; eG[i] = 255; eB[i] = 255;
		}
		
		gc.fillStyle = "rgb("+eR[i]+","+eG[i]+","+eB[i]+")";
		gc.fillRect(eX[i], eY[i], eW[i], eH[i]);
	}
	explodeTimer--;
	if(explodeTimer < 1){
		lives--;
		cS = 0;
		clearInterval(gameInterval);
		if(lives < 0){
			musicSnd.pause();
			gameState = 4;
			cAnimateFrame = 0;
			gameInterval = setInterval(gameEngine, 100);
		}else{
			gameInterval = setInterval(gameEngine, 25);
			gameState = 2;
		}
	}
}
function gameOver(){
	cAnimateFrame++;
	gameOverCount++;
	if(cAnimateFrame>2) cAnimateFrame = 0;
	if(cAnimateFrame==0) fc.drawImage(spr_gameover, 0, 0, 202, 51, 206, 200, 202, 51);
	if(cAnimateFrame==1) fc.drawImage(spr_gameover, 0, 51, 202, 51, 206, 200, 202, 51);
	if(cAnimateFrame==2) fc.drawImage(spr_gameover, 0, 102, 202, 51, 206, 200, 202, 51);
	if(gameOverCount==25) {gameState = 5; fc.clearRect(206, 200, 202, 51);  displayName();}
}
function displayName(){
	if(userType=="user"){nameString = userName;}
	var textX = (WIDTH/2)-(nameString.length/2 * 13);
	var nameVar;
	if(nameString.length<1){
		nameVar = "_";
	}else{
		nameVar = nameString;
	}
	fc.clearRect(textX-52, 180, nameString.length*13+90, 50);
	fc.font = 'normal 26px monospace';
	fc.fillStyle = 'white';
	fc.fillText(nameVar, textX, 200);


	fc.font = 'normal 26px monospace';
	fc.fillStyle = 'white';
	textX = (WIDTH/2)-(18/2 * 13);
	fc.fillText("Submit Your Score", textX, 150);
	
	fc.font = 'normal 18px monospace';
	fc.fillStyle = 'white';
	textX = (WIDTH/2)-(26/2 * 9);
	fc.fillText("press enter when finished", textX, 250);
}
function submitScore(){
	clear(fc);
	clearInterval(gameInterval);
	clear(gc);
	if(score > 1000){
		fc.drawImage(scr_code,0,0);
		setTimeout('titleScroll()', 1000)
	}else{		
		titleScroll();
	}
	resetVars();
}
function updateNameString(keyPressed){
	switch(keyPressed){
		case 8://backspace
		nameString = nameString.substring(0, nameString.length -1); displayName();
		break;
		case 48:// 0
		nameString = nameString + '0'; displayName();
		break;
		case 49:// 1
		nameString = nameString + '1'; displayName();
		break;
		case 50:// 2
		nameString = nameString + '2'; displayName();
		break;
		case 51:// 3
		nameString = nameString + '3'; displayName();
		break;
		case 52:// 4
		nameString = nameString + '4'; displayName();
		break;
		case 53:// 5
		nameString = nameString + '5'; displayName();
		break;
		case 54:// 6
		nameString = nameString + '6'; displayName();
		break;
		case 55:// 7
		nameString = nameString + '7'; displayName();
		break;
		case 56:// 8
		nameString = nameString + '8'; displayName();
		break;
		case 57:// 9
		nameString = nameString + '9'; displayName();
		break;
		case 65:// a
		nameString = nameString + 'a'; displayName();
		break;
		case 66:// b
		nameString = nameString + 'b'; displayName();
		break;
		case 67:// c
		nameString = nameString + 'c'; displayName();
		break;
		case 68:// d
		nameString = nameString + 'd'; displayName();
		break;
		case 69:// e
		nameString = nameString + 'e'; displayName();
		break;
		case 70:// f
		nameString = nameString + 'f'; displayName();
		break;
		case 71:// g
		nameString = nameString + 'g'; displayName();
		break;
		case 72:// h
		nameString = nameString + 'h'; displayName();
		break;
		case 73:// i
		nameString = nameString + 'i'; displayName();
		break;
		case 74:// j
		nameString = nameString + 'j'; displayName();
		break;
		case 75:// k
		nameString = nameString + 'k'; displayName();
		break;
		case 76:// l
		nameString = nameString + 'l'; displayName();
		break;
		case 77:// m
		nameString = nameString + 'm'; displayName();
		break;
		case 78:// n
		nameString = nameString + 'n'; displayName();
		break;
		case 79:// o
		nameString = nameString + 'o'; displayName();
		break;
		case 80:// p
		nameString = nameString + 'p'; displayName();
		break;
		case 81:// q
		nameString = nameString + 'q'; displayName();
		break;
		case 82:// r
		nameString = nameString + 'r'; displayName();
		break;
		case 83:// s
		nameString = nameString + 's'; displayName();
		break;
		case 84:// t
		nameString = nameString + 't'; displayName();
		break;
		case 85:// u
		nameString = nameString + 'u'; displayName();
		break;
		case 86:// v
		nameString = nameString + 'v'; displayName();
		break;
		case 87:// w
		nameString = nameString + 'w'; displayName();
		break;
		case 88:// x
		nameString = nameString + 'x'; displayName();
		break;
		case 89:// y
		nameString = nameString + 'y'; displayName();
		break;
		case 90:// z
		nameString = nameString + 'z'; displayName();
		break;
		case 13://Enter
		gameState = 6;
		submitScore();
		break;
	}
	if(nameString.length > 10) {nameString = nameString.substring(0,10); displayName();}

}
function updateText(){
	//update feed
	fc.clearRect(30, 484, 150, 70);
 	fc.font = 'normal 18px sans-serif';
	fc.fillStyle = 'white';
	fc.fillText('FEED: '+feed, 34, 508);
	//update lives
	for(var i=0; i<lives; i++){
		fc.drawImage(spr_life, 130+(i*20), 484);
	}
	//update score
	fc.clearRect(500, 36, 80, 25);
	fc.font = 'normal 14px sans-serif';
	fc.fillStyle = 'white';
	fc.fillText(score, 500, 50);
}
function setNewGame(){
	clear(fc);
	fc.drawImage(scr_frame, 0, 0);
	dX = 0;
	leftPressed = false;
	rightPressed = false;
	gameInterval = setInterval(gameEngine, 25);//initialize the game timer
	gameState = 2;
	if(soundOn)musicSnd.play();
}
//Key Press Functions ==============================================================================
function onKeyDown(evt) {
	if(gameState==5){
		if(userType=="user"){
			if(evt.keyCode==13){
				gameState = 6;
				submitScore();				
			}
		}else{
			updateNameString(evt.keyCode);	
		}
	}
	if(lastKeyPressed && lastKeyPressed==evt.keyCode) return false;
	lastKeyPressed = evt.keyCode;
	if(evt.keyCode == 39 && gameState==2) rightPressed = true;
	if(evt.keyCode == 37 && gameState==2) leftPressed = true;
	if(evt.keyCode == 38 && gameState==2) {//upPressed
		if(!upPressed){upPressed = true;}else{upPressed = false;}
	}
	if(evt.keyCode == 40 && gameState==2) downPressed = true;
	if(gameState==0 && evt.keyCode!=13) {//
		gameState=97;
		clearTimeout(titleTimer);
		gameTransition();
	}
	
	if(evt.keyCode == 83 && gameState!=5){//turn sounds on off pressing S
		toggleSound();
	}
	return false;
}
function toggleSound(){
	if(soundOn){
		soundOn = false;
		musicSnd.pause();
	}else{
		soundOn = true;
		if(gameState==2) musicSnd.play();
	}
}
function onKeyUp(evt) {
	lastKeyPressed = null;
	if(evt.keyCode == 39) rightPressed = false;
	if(evt.keyCode == 37) leftPressed = false;
//	if(evt.keyCode == 38) upPressed = false;
	if(evt.keyCode == 40) downPressed = false;
}
$("#frontCanvas").mousedown(function(e){ $(this).focus(); return false; })
$("#frontCanvas").keydown(onKeyDown);
$("#frontCanvas").keyup(onKeyUp);

